package com.zh.mall.controller;

import com.zh.mall.common.CommonPage;
import com.zh.mall.common.CommonResult;
import com.zh.mall.mbg.model.UmsAdmin;
import com.zh.mall.service.UmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理员Controller
 * @author ZhengHao
 * Created At 2022/7/15.
 **/
@RestController
@RequestMapping("/umsAdmin")
public class UmsAdminController {
    @Autowired
    private UmsService umsAdminService;

    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminController.class);

    @GetMapping("/listAll")
    public CommonResult<List<UmsAdmin>> getUmsAdminList(){
        return CommonResult.success(umsAdminService.listAllUmsAdmin());
    }

    @PostMapping("/create")
    public CommonResult craeteUmsAdmin(@RequestBody UmsAdmin umsAdmin){
        CommonResult commonResult;
        int count = umsAdminService.craeteUmsAdmin(umsAdmin);
        if (count == 1){
            commonResult = CommonResult.success(umsAdmin);
            LOGGER.debug("createUmsAdmin:{}",umsAdmin);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createUmsAdmin failed:{}",umsAdmin);
        }
        return commonResult;
    }

    @PostMapping("/update/{id}")
    public CommonResult updateUmsAdmin(@PathVariable("id") Long id,@RequestBody UmsAdmin umsAdmin){
        CommonResult commonResult;
        int count = umsAdminService.updateUmsAdmin(id,umsAdmin);
        if (count == 1){
            commonResult = CommonResult.success(umsAdmin);
            LOGGER.debug("updateUmsAdmin success:{}",umsAdmin);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateUmsAdmin failed:{}",umsAdmin);
        }
        return commonResult;
    }

    @GetMapping("/delete/{id}")
    public CommonResult deleteUmsAdmin(@PathVariable("id")Long id){
        int count = umsAdminService.deleteUmsAdmin(id);
        if (count == 1){
            LOGGER.debug("deleteUmsAdmin success :id={}",id);
            return CommonResult.success(null);
        } else {
            LOGGER.debug("deleteUmsAdmin failed :id={}",id);
            return CommonResult.failed("操作失败");
        }
    }

    @GetMapping("/list")
    public CommonResult listUmsAdmin(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        List<UmsAdmin> umsAdminList = umsAdminService.listUmsAdmin(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(umsAdminList));
    }

    @GetMapping("/{id}")
    public CommonResult umsAdmin(@PathVariable("id")Long id){
        return CommonResult.success(umsAdminService.getUmsAdmin(id));
    }
}